package com.education.platform.management.controller.article;


import com.education.platform.common.enums.SysArticleTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.entity.ArticleDO;
import com.education.platform.dao.model.ArticleModel;
import com.education.platform.management.enums.PushTypeEnum;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.ArticleService;
import com.education.platform.management.vo.request.ArticleQueryVO;
import com.education.platform.management.vo.request.ArticleUpdateVO;
import com.education.platform.management.vo.request.OnShelfVO;
import com.education.platform.management.vo.response.ArticleDetailResponseVO;
import com.education.platform.management.vo.response.ArticlePageInfoVO;
import com.education.platform.management.vo.response.ArticleResponseVO;
import com.education.platform.management.vo.response.SysDictionaryResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
* describe: 文章
* @author :
* @date: 2020-07-06 09:23:16.228
**/
@RestController
@Api("文章")
@RequestMapping("article")
@Validated
public class ArticleController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ArticleController.class);

	@Autowired
    private ArticleService articleService;
	@Autowired
    private PushUtil pushUtil;

    /**
     * 分页查询
     * @param articleQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询文章")
    @PostMapping("getPageList")
    public ArticlePageInfoVO selectPageList(@RequestBody @Valid ArticleQueryVO articleQueryVO) {
        ArticleModel article = BeanUtil.convert(articleQueryVO, ArticleModel.class);
        article.setDeleted(false);
        Page<ArticleResponseVO> page = PageHelper.startPage(articleQueryVO.getPageNum(), articleQueryVO.getPageSize());
        List<ArticleModel> articleList = articleService.getArticleList(article);
        ArticlePageInfoVO articlePageInfo = new ArticlePageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), articlePageInfo);
        articlePageInfo.setPageNum(articleQueryVO.getPageNum());
        List<ArticleResponseVO> voList = BeanUtil.convert(articleList, ArticleResponseVO.class);
        articlePageInfo.setList(voList);
        return articlePageInfo;
    }

    /**
     * 新增文章
     * @param vo
     * @return
     */
    @ApiOperation(value = "新增/修改 文章")
    @PostMapping("save")
    public void save(@RequestBody @Valid ArticleUpdateVO vo) {
        ArticleDO articleDO = BeanUtil.convert(vo, ArticleDO.class);
        articleService.save(articleDO);
    }

    /**
     * 查询文章详情
     * @param
     * @return
     */
    @ApiOperation(value = "查询文章详情")
    @GetMapping("getDetailsById")
    public ArticleDetailResponseVO getDetailsById(@RequestParam Integer id) {

        ArticleDO articleDO = articleService.getById(id);
        if(articleDO == null){
            throw new BusinessException("文章不存在");
        }
        ArticleDetailResponseVO responseVO = BeanUtil.convert(articleDO, ArticleDetailResponseVO.class);
        return responseVO;
    }

    /**
     * 文章类型
     * @param
     * @return
     */
    @ApiOperation(value = "文章类型")
    @GetMapping("getArticleType")
    public List<SysDictionaryResponseVO> getArticleType() {
        List<SysDictionaryResponseVO> responseVOS = new ArrayList<>(4);
        SysDictionaryResponseVO vo;
        for(SysArticleTypeEnum type: SysArticleTypeEnum.values()){
            vo  = new SysDictionaryResponseVO();
            vo.setCode(type.getValue());
            vo.setName(type.getDes());
            responseVOS.add(vo);
        }
        return responseVOS;
    }


    @ApiOperation(value = "上架")
    @PostMapping("onShelf")
    public void onShelf(@RequestBody OnShelfVO onShelfVO) {
        List<Integer> ids = onShelfVO.getIds();
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<ArticleDO> updateList = new ArrayList<>(ids.size());
        ArticleDO articleDO;
        for(Integer id:ids){
            articleDO = new ArticleDO();
            articleDO.setId(id);
            articleDO.setPublish(true);
            updateList.add(articleDO);
        }
        articleService.updateInBatch(updateList);
        //推送消息
        if(StringUtils.isNotEmpty(onShelfVO.getPushTitle())&&StringUtils.isNotEmpty(onShelfVO.getPushText())){
            for(Integer id:ids){
                pushUtil.push( PushTypeEnum.ARTICLE, String.valueOf(id), onShelfVO.getPushTitle(),onShelfVO.getPushText() );
            }
        }
    }
    @ApiOperation(value = "下架")
    @PostMapping("downShelf")
    public void downShelf(@RequestBody  List<Integer> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<ArticleDO> updateList = new ArrayList<>(ids.size());
        ArticleDO articleDO;
        for(Integer id:ids){
            articleDO = new ArticleDO();
            articleDO.setId(id);
            articleDO.setPublish(false);
            updateList.add(articleDO);
        }
        articleService.updateInBatch(updateList);
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping("delete")
    public void delete(@RequestBody List<Integer>ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("参数为空");
        }
        List<ArticleDO> updateList = new ArrayList<>(ids.size());
        ArticleDO update;
        for(Integer id:ids){
            update = new ArticleDO();
            update.setId(id);
            update.setDeleted(true);
            updateList.add(update);
        }
        articleService.updateInBatch(updateList);
    }
}
